package com.op.teachingliteapp.activity;

import net.loonggg.bar.ListViewAdapter;
import net.loonggg.bar.MainFragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.op.teachingliteapp.BuildConfig;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.cloud_disk;
import com.op.teachingliteapp.fragment.settings;
import com.op.teachingliteapp.fragment.course.CourseFragment;

public class HomeActivity extends Activity {
	//测试语句
	/*
	 * 张展康
	 */
	private static final String TAG = HomeActivity.class.getSimpleName();
	private static int backChoice=0;
	public static int getBackChoice() {
		return backChoice;
	}
	public static void setBackChoice(int backChoice) {
		HomeActivity.backChoice = backChoice;
	}

	private boolean isExit = false;
	private TextView mainTitle; 
	private Button YunKeTang; // cloudClassBtn
	private Button cloudDiskBtn; // 云盘
	private Button settingsBtn; // settingsBtn
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (BuildConfig.DEBUG) {
			Log.i(TAG, "oncreate");
		}
		//requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.activity_home);
		mainTitle=(TextView) findViewById(R.id.top_view_one);


		if(getFragmentManager().findFragmentById(R.id.main_show)==null){
			getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.main_show,new CourseFragment()).commit();
		}

		//登录
		findViewById(R.id.load).setOnTouchListener(new OnTouchListener() {

			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction()==MotionEvent.ACTION_DOWN){
					//保留有用，不要删
				}
				if(event.getAction()==MotionEvent.ACTION_UP){
					Toast.makeText(getApplicationContext(), "登录", Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});

		//云课堂
		YunKeTang=(Button) findViewById(R.id.buttom_home);
		YunKeTang.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mainTitle.setText("课程选择");
				YunKeTang.setBackgroundResource(R.drawable.buttom_home2);
				cloudDiskBtn.setBackgroundResource(R.drawable.buttom_cloud3);
				settingsBtn.setBackgroundResource(R.drawable.buttom_set3);
				getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new CourseFragment()).commit();

			}
		} );

		//云盘
		cloudDiskBtn=(Button) findViewById(R.id.buttom_cloud3);
		cloudDiskBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mainTitle.setText("云盘");
				cloudDiskBtn.setBackgroundResource(R.drawable.buttom_cloud2);
				YunKeTang.setBackgroundResource(R.drawable.buttom_home3);
				settingsBtn.setBackgroundResource(R.drawable.buttom_set3);
				Toast.makeText(getApplicationContext(), "冲哥正在熬夜开发", Toast.LENGTH_SHORT).show();
				getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new cloud_disk()).commit();
			}
		});


		//设置
		settingsBtn= (Button) findViewById(R.id.buttom_set3);
		settingsBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mainTitle.setText("设置");
				settingsBtn.setBackgroundResource(R.drawable.buttom_set2);
				YunKeTang.setBackgroundResource(R.drawable.buttom_home3);
				cloudDiskBtn.setBackgroundResource(R.drawable.buttom_cloud3);
				getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new settings()).commit();
			}
		});
		//返回
		//findViewById(R.id.back).setVisibility(View.GONE);
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				outback(getBackChoice());

			}
		});


	}
	private void outback(int i){
		switch (i) {
		case 1:	//返回到云课堂，清空课程列表已选择的内容
			ListViewAdapter.getCourseList().clear();
			getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new CourseFragment()).commit();
			setBackChoice(0);
			break;
		case 2:	

			break;
		default:
			break;		
		}
		backChoice=0;
	}




	/*
	 * 锁定虚拟返回键  重写Activity中onKeyDown（）方法
	 * 代码顺序别改，改了功能会出错的。
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {// 当keyCode等于退出事件值时
			if(getBackChoice()==0){
				ToQuitTheApp();		
			}	
			outback(getBackChoice());
			return false;
		} 
		else {
			return super.onKeyDown(keyCode, event);
		}
		

	}




	private void ToQuitTheApp() {
		if (isExit) {
			// ACTION_MAIN with category CATEGORY_HOME 启动主屏幕
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);// 使虚拟机停止运行并退出程序
			finish();
			//			android.os.Process.killProcess(android.os.Process.myPid());
		} else {
			isExit = true;
			Toast.makeText(HomeActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 3000);// 3秒后发送消息
		}
	}

	//创建Handler对象，用来处理消息
	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {//处理消息
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			isExit = false;
		}
	};




	@Override
	protected void onStart() {
		System.out.println("stop");
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		System.out.println("out");
		super.onDestroy();
	}
}
